package openjdk.future;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created by boot on 1/11/16.
 */
public class TestFuture {
    static class Job<Object> implements Callable<Object> {

        /**
         * Computes a result, or throws an exception if unable to do so.
         *
         * @return computed result
         * @throws Exception if unable to compute a result
         */
        @Override
        public Object call() throws Exception {
            return loadData();
        }

        private Object loadData() throws InterruptedException {
            System.out.println("enter the loadData method.");
            Thread.sleep(5000);
            return (Object) (Thread.currentThread().getId() + "-" + System.currentTimeMillis());
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask future = new FutureTask(new Job<Object>());
        new Thread(future).start();
        // do something else....
        String result = (String) future.get();
        System.out.println(result);

    }
}
